package controllers

import (
	"egin/app/request"
	"egin/app/service"
	"egin/pkg/redigo"
	"egin/pkg/response"
	"egin/pkg/utils"
	"egin/pkg/vali"
	"encoding/json"
	"net/http"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
)

//MemberIndexHtml 管理员列表页
func MemberIndexHtml(c *gin.Context) {

	c.HTML(http.StatusOK, "member/index.tmpl", nil)
}

//MemberCreateHtml 添加管理员页面
func MemberCreateHtml(c *gin.Context) {
	c.HTML(http.StatusOK, "member/create.tmpl", nil)
}

//MemberEditHtml 修改管理员页面
func MemberEditHtml(c *gin.Context) {
	id := c.Param("id")
	member, _ := service.GetMemberAndRole(id)
	memberJson, err := json.Marshal(member)
	if err != nil {
		zap.L().Error("MemberEditHtml json err", zap.Error(err))
	}

	c.HTML(http.StatusOK, "member/edit.tmpl", gin.H{"member": string(memberJson), "id": utils.StrToInt(c.Param("id"))})
}

//MemberIndex  获取管理员列表数据
func MemberIndex(c *gin.Context) {

	list, err := service.GetMemberList(c)
	if err == nil {
		response.SuccessWithData(list, c)
	} else {
		response.ErrorWithMessage("服务器错误", c)
	}
}

//MemberCreate 添加管理员
func MemberCreate(c *gin.Context) {

	var param request.MemberQs
	param.CheckPhone()

	if err := c.ShouldBind(&param); err != nil {
		errStr := vali.ValidErrMsg(&param, err.(validator.ValidationErrors))
		response.ErrorWithMessage(errStr, c)
		return
	}

	err := service.MemberAdd(param)
	if err == nil {
		response.SuccessWithMessage("添加成功", c)
	} else {
		response.ErrorWithMessage("添加失败", c)
	}
}

//MemberEdit 修改管理员
func MemberEdit(c *gin.Context) {
	var param request.MemberEditQs
	param.CheckPhone()
	if err := c.ShouldBind(&param); err != nil {
		errStr := vali.ValidErrMsg(&param, err.(validator.ValidationErrors))
		response.ErrorWithMessage(errStr, c)
		return
	}

	err := service.MemberEdit(param)
	if err == nil {
		conn := redigo.GetPool().Get()
		defer conn.Close()
		conn.Do("hdel", "admin_rbac", param.Id)
		conn.Do("hdel", "admin_menu", param.Id)
		response.SuccessWithMessage("修改成功", c)
	} else {
		response.ErrorWithMessage("修改失败", c)
	}
}

func MemberDelete(c *gin.Context) {
	err := service.MemberDelete(c)
	if err == nil {
		conn := redigo.GetPool().Get()
		defer conn.Close()
		conn.Do("hdel", "admin_rbac", c.Param("id"))
		conn.Do("hdel", "admin_menu", c.Param("id"))
		response.SuccessWithMessage("删除成功", c)
	} else {
		response.ErrorWithMessage("删除失败", c)
	}
}

func MemberUserInfoHtml(c *gin.Context) {
	sesssion := sessions.Default(c)
	id := sesssion.Get("id")
	member, _ := service.GetMember(id.(string))
	member_json, _ := json.Marshal(member)

	c.HTML(http.StatusOK, "member/user_info.tmpl", gin.H{
		"member": string(member_json),
	})
}

func MemberUserInfo(c *gin.Context) {
	sesssion := sessions.Default(c)
	id := sesssion.Get("id")

	var param request.MemberEditQs

	param.Id = utils.StrToUInt(id.(string))

	param.CheckPhone()

	if err := c.ShouldBind(&param); err != nil {
		errStr := vali.ValidErrMsg(&param, err.(validator.ValidationErrors))
		response.ErrorWithMessage(errStr, c)
		return
	}
	err := service.MemberUserInfo(param)
	if err == nil {
		sesssion.Set("ls_load", "1")
		sesssion.Save()

		response.SuccessWithMessage("修改成功", c)
	} else {
		response.ErrorWithMessage("修改失败", c)
	}
}

func MemberUserPwdHtml(c *gin.Context) {
	sesssion := sessions.Default(c)
	id := sesssion.Get("id")

	member, _ := service.GetMember(id.(string))

	c.HTML(http.StatusOK, "member/user_pwd.tmpl", gin.H{
		"username": member.Username,
	})
}

func MemberUserPwd(c *gin.Context) {
	sesssion := sessions.Default(c)
	id := sesssion.Get("id")

	var param request.MemberPwd

	param.Id = utils.StrToUInt(id.(string))

	if err := c.ShouldBind(&param); err != nil {
		errStr := vali.ValidErrMsg(&param, err.(validator.ValidationErrors))
		response.ErrorWithMessage(errStr, c)
		return
	}

	err := service.MemberUserPwd(param)
	if err == nil {
		response.SuccessWithMessage("密码修改成功", c)
	} else {
		response.ErrorWithMessage(err.Error(), c)
	}
}
